import {Injectable} from '@nestjs/common';
import {InjectRepository} from "@nestjs/typeorm"; // 用于注入 TypeORM 的 Repository
import {Repository} from "typeorm"; // TypeORM 的 Repository 类，用于操作数据库
import {Users} from "../../entity/users.entity";
import {UsersInterface} from "../../interface/users/users.interface"; // 用户实体类，对应数据库中的用户表

@Injectable()
export class UsersService {
    constructor(
        // 注入 Users 实体的 Repository
        @InjectRepository(Users)
        // 声明一个私有的只读属性，用于操作 Users 实体
        private readonly usersRepository: Repository<Users>
    ) {
    }

    async findAll(json: UsersInterface = {}) {
        const {page = 1, pageSize = 10} = json;
        return await this.usersRepository.findAndCount({
            skip: (page - 1) * pageSize,
            take: pageSize,
            order: {id: 'DESC'}
        })
    }

    async findDetail(id) {
        return await this.usersRepository.findBy({id})
    }

    async findOne(where) {
        return await this.usersRepository.findOne({where});
    }

    async save(createUserDto) {
        const user = this.usersRepository.create(createUserDto)
        return await this.usersRepository.save(user);
    }

    async update(id: number, updateUserDto) {
        return await this.usersRepository.update(id, updateUserDto);
    }

    async del(id) {
        return await this.usersRepository.delete(id)
    }
}
